package org.gitos.cloud.comet.logic.recMQLogic;

import io.netty.channel.ChannelHandlerContext;

import org.gitos.cloud.comet.tcp.ConnectionHolder;
import org.gitos.cloud.domain.comm.Constant;
import org.gitos.cloud.domain.mq.MqMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by shizhengchu on 15/12/24.
 */
public class SndContentEvent implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(SndContentEvent.class);
    private MqMsg msg;

    public SndContentEvent(MqMsg msg) {
        this.msg = msg;
    }

    @Override
    public void run() {
        String channelId = msg.getChannelId();
        ChannelHandlerContext ctx = ConnectionHolder.CHANNEL_HANDLER_CONTEXT_MAP.get(channelId);
        if (ctx == null) {
            log.error("cant not find ctx at channelId:{}====\nmsg:{}", channelId, msg.toString());
            return;
        }
        ctx.pipeline().writeAndFlush(msg.getBucket());
        if (msg.getBucket().getCmd() == Constant.CMD_DOWN_CLOSE) {
            ctx.pipeline().close();
        }
    }
}
